package net.sx.minemodule.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import net.sx.minemodule.R;
import net.sx.minemodule.mvp.bean.LogisticsCompany;

import java.util.List;

/**
 * Created by Administrator 可爱的路人 on 2019/7/6.
 * Email:513421345@qq.com
 * TODO
 */
public class LogisticsSelectAdapter extends RecyclerView.Adapter<LogisticsSelectAdapter.Holder> {
    private int selectedPosition = -1;
    private Context context;
    private List<LogisticsCompany> data;
    private OnSelectedListener listener;

    public void setData(List<LogisticsCompany> data) {
        this.data = data;
    }

    public LogisticsSelectAdapter(Context context) {
        this.context = context;
    }

    public LogisticsSelectAdapter(Context context, OnSelectedListener listener) {
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.mine_item_logistics_select, viewGroup, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder viewHolder, int i) {
        if (i == selectedPosition) {
            viewHolder.iv.setImageResource(R.mipmap.progress_sel);
        } else {
            viewHolder.iv.setImageResource(R.mipmap.icon_unselected);
        }
        viewHolder.tvName.setText(data.get(i).getName());
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public LogisticsCompany getSelectCompany() {
        if (data != null && selectedPosition >= 0 && selectedPosition < data.size()) {
            return data.get(selectedPosition);
        } else {
            return null;
        }
    }

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView iv;
        TextView tvName;

        public Holder(@NonNull View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv_item_logistics_select);
            tvName = itemView.findViewById(R.id.tv_item_logistics_select_name);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (selectedPosition == getAdapterPosition()) {
                selectedPosition = -1;
            } else {
                selectedPosition = getAdapterPosition();
            }
            if (listener != null) {
                if (selectedPosition < 0 || selectedPosition >= getItemCount()) {
                    listener.onSelected(null);
                } else {
                    listener.onSelected(data.get(selectedPosition));
                }
            }
            notifyDataSetChanged();
        }
    }

    public static interface OnSelectedListener {
        void onSelected(LogisticsCompany company);
    }

}
